package top.atluofu.common.valid;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.HashSet;
import java.util.Set;

/**
 * @ClassName: ListValueConstraintValidator
 * @description: TODO
 * @author: 有罗敷的马同学
 * @datetime: 2022Year-10Month-30Day-23:31
 * @Version: 1.0
 */
public class ListValueConstraintValidator implements ConstraintValidator<ListValue, Integer> {

    private Set<Integer> setValues = new HashSet<>();

    @Override
    public void initialize(ListValue constraintAnnotation) {
        int[] vals = constraintAnnotation.vals();
        for (int i = 0; i < vals.length; i++) {
            setValues.add(vals[i]);
        }
    }


    @Override
    public boolean isValid(Integer integer, ConstraintValidatorContext constraintValidatorContext) {
        return setValues.contains(integer);
    }
}
